/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package model.dao.benhan;

import java.util.Date;
import java.util.List;
import model.dao.AbstractQuery;
import model.dao.ProjectQuery;
import model.entity.BenhAn;
import model.dao.benh.BenhDAO;
import model.dao.vatnuoi.VatNuoiDAO;
import model.entity.BacSi;
import model.entity.Benh;
import model.entity.BenhAn;
import model.entity.ChungLoai;
import model.entity.KhachHang;
import model.entity.Loai;
import model.entity.VatNuoi;
import model.initialize.Initialize;
import model.util.HibernateUtil;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;

/**
 *
 * @author luanp_000
 */
public class BenhAnDAO {
    private static String LOAD_ALL = "FROM BenhAn";
    private static String LOAD_BY_ID = "FROM BenhAn AS benhAn WHERE benhAn.baMa =";
    private static String LOAD_BY_TEN = "FROM BenhAn AS benhAn WHERE benhAn.benh.benhTen =";
    /**
     * Load tất cả bệnh án vào 1 List
     *
     * @return List danh sách tất cả các bệnh án. Mỗi 1 đối tượng trong List
     * trả về là 1 dòng (row) trong database.
     */
    public static final List<BenhAn> loaddAllBenhAn() {
      /**  return AbstractQuery.loadObject(ProjectQuery.LOAD_ALL_BENHAN);*/
	   Session session = HibernateUtil.getSessionFactory().openSession();
        List<BenhAn> resultList;
        try {
            session.beginTransaction();
            resultList = session.createQuery("FROM BenhAn AS benhAn").list();
            for (BenhAn benhAn : resultList) {
                Hibernate.initialize(benhAn.getBenh());
                Hibernate.initialize(benhAn.getVatNuoi());
            }
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
        }
        return resultList;
    }

    public static final BenhAn load1BenhAn(int baMa) {
     /**   List list = AbstractQuery.loadObject(ProjectQuery.LOAD_1_BENHAN + baMa);
        return (BenhAn) list.get(0); */
		
	Session session = HibernateUtil.getSessionFactory().openSession();
        List<BenhAn> resultList;
        try {
            session.beginTransaction();
            resultList = session.createQuery(LOAD_BY_ID + baMa).list();
            for (BenhAn benhAn : resultList) {
                Initialize.initializeBenhAn(benhAn);
            }
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw  new HibernateException(e);
        }finally{
            session.close();
        }
        return resultList.get(0);
    }
	
	/**
     * Liệt kê theo tên bệnh 
     * @param tenBenh
     * @return 
     */
    
    public static final List<BenhAn> loadBenhAnTheoTenBenh(String tenBenh) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        List<BenhAn> resultList;
        try {
            session.beginTransaction();
            resultList = session.createQuery(LOAD_BY_TEN + "'"+tenBenh+"'").list();
            for (BenhAn benhAn : resultList) {
                Hibernate.initialize(benhAn.getBenh());
                Hibernate.initialize(benhAn.getVatNuoi());
            }
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw  new HibernateException(e);
        }finally{
            session.close();
        }
        return resultList;
    }
	
	/**
     * Liệt kê theo mã Bệnh Án
     * @param maBA
     * @return tên bệnh án cần tìm
     */
     public static List loadBenhAnTheoMaBenhAn(int maBA) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<BenhAn> resultList;
        try {
            session.beginTransaction();
            resultList = session.createQuery(LOAD_BY_ID + "'"+maBA+"'").list();
            for (BenhAn benhAn : resultList) {
                Hibernate.initialize(benhAn.getBenh());
                Hibernate.initialize(benhAn.getVatNuoi());
            }
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw  new HibernateException(e);
        }finally{
            session.close();
        }
        return resultList;
    }


    /**
     * Thêm mới 1 bệnh án
     *
     * @param ba đối tượng bệnh án cần thêm
     */
	 public static  final int insertBenhAn(BenhAn ban){
        Session session = HibernateUtil.getSessionFactory().openSession();
        int id;
        try {
            //Câu này load xuong csdl
//            VatNuoiDAO.insertVatNuoi(ban.getVatNuoi());
            BenhDAO.insertBenh(ban.getBenh());
            session.beginTransaction();
            session.save(ban);
            session.getTransaction().commit();
            
            id = ban.getBaMa();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        }finally {
            session.close();
        }
        return id;
    }


    /**
     * Sửa thông tin 1 bệnh án.
     *
     * @param ba đối tượng bệnh án sẽ được sửa thông tin.
     */
    public static final void updateBenhAn(BenhAn ba) {
         Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.update(ba);
            session.getTransaction().commit();
        } catch (HibernateException e) {
            session.getTransaction().rollback();
            throw new HibernateException(e);
        } finally {
            session.close();
    }
    }

    /**
     * Xóa 1 bệnh án.
     *
     * @param baMa mã đối tượng bệnh án sẽ bị xóa.
     */
    public static final void deleteBenhAn(int baMa) {
        AbstractQuery.modifyObject(3, (BenhAn) AbstractQuery.loadObject(ProjectQuery.LOAD_1_BENHAN + baMa).get(0));
    }
	
	/**
     * Tạo bệnh án
     * @param args 
     */
    public static final boolean taoBenhAnMoi(int maVatNuoi, int maBenh, Date baNgayBatDau, Date baNgayKetThuc, String baGhiChu){
        VatNuoi vatNuoi = VatNuoiDAO.timTheoMaVatNuoi(maVatNuoi);
        Benh benh = BenhDAO.timKiemTheoMaBenh(maBenh).get(0);
        if(vatNuoi != null){
            if(benh != null){
                BenhAn benhAn = new BenhAn();
                benhAn.setVatNuoi(vatNuoi);
                benhAn.setBenh(benh);
                benhAn.setBaNgayBd(baNgayBatDau);
                benhAn.setBaNgayKt(baNgayKetThuc);
                benhAn.setBaGhiChu(baGhiChu);
                benhAn.setBaTrangThai("Còn bệnh");
//        insertBenhAn(benhAn);
                Session session = HibernateUtil.getSessionFactory().getCurrentSession();
                session.getTransaction().begin();
                session.save(benhAn);
                session.getTransaction().commit();
                return  true;
            }
        }
        return false;
    }
	
	/**
     * UpDate lại thông tin
     * @param args 
     */
    public static final boolean SuaBenhAn(int maBA,int maVatNuoi, int maBenh, Date baNgayBatDau, Date baNgayKetThuc, String baTrangThai, String baGhiChu){
        VatNuoi vatNuoi = VatNuoiDAO.timTheoMaVatNuoi(maVatNuoi);
        Benh benh = BenhDAO.timKiemTheoMaBenh(maBenh).get(0);
        BenhAn benhAn = BenhAnDAO.load1BenhAn(maBA);
        if(vatNuoi != null){
            benhAn.setVatNuoi(vatNuoi);
        }
        if(benh != null){
            benhAn.setBenh(benh);
        }
        benhAn.setBaNgayBd(baNgayBatDau);
        benhAn.setBaNgayKt(baNgayKetThuc);
        benhAn.setBaTrangThai(baTrangThai);
        benhAn.setBaGhiChu(baGhiChu);
        
        updateBenhAn(benhAn);
        return true;
    }
    
    
     public static final boolean xoaBenhAn(int maBA, String baTenBenh, Date baNgayBatDau, Date baNgayKetThuc, String baTrangThai, String baGhiChu){
        BenhAn benhAn = new BenhAn();
        Benh benh = new Benh();
        //dòng này để update lại trong database
//        benh.setBenhMa(15);
        benh.setBenhTen(baTenBenh);
        
        benhAn.setBaMa(maBA);
        benhAn.setBenh(benh);
        benhAn.setBaNgayBd(baNgayBatDau);
        benhAn.setBaNgayKt(baNgayKetThuc);
        benhAn.setBaTrangThai(baTrangThai);
        benhAn.setBaGhiChu(baGhiChu);
        
         deleteBenhAn(maBA);
        return true;
    }
    
}
